package com.testproject.run;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.testproject.domain.Division;
import com.testproject.domain.Employee;
import com.testproject.domain.Roles;
import com.testproject.domain.Users;

public class Runner {

	public static void main(String[] args) {
		
		Calendar c1 = GregorianCalendar.getInstance();
		c1.set(2000, 1, 30);

		Transaction tx = null;
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
				.applySettings(configuration.getProperties())
				.buildServiceRegistry();
		SessionFactory sessionFactory = configuration
				.buildSessionFactory(serviceRegistry);
		Session s = sessionFactory.openSession();
		tx = s.beginTransaction();

		Division division = new Division();
		division.setName("TestDivision");
		s.save(division);
		Employee emp1 = new Employee("MyFirstName", "MyLastName", 200.9, c1, true);
		Employee emp2 = new Employee("MyFirstName1", "MyLastName1", 300.9, c1, false);
		emp1.setDivision(division);
		emp2.setDivision(division);
		s.save(emp1);
		s.save(emp2);
		
		Roles roles = new Roles();
		roles.setName("TestDivision");
		s.save(roles);
		Users us1 = new Users("MyFirstName", "MyLastName", "ttt");
		Users us2 = new Users("MyFirstName1", "MyLastName1", "ttt");
		us1.setRoles(roles);
		us2.setRoles(roles);
		s.save(us1);
		s.save(us2);
		
		ApplicationContext context = 
	             new ClassPathXmlApplicationContext("spring.xml");


		 Employee obj = (Employee) context.getBean("helloWorld");

	      obj.getFirstName();
	      
	      

		tx.commit();
		s.close();
	}

}
